<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author Bidding
 * @see 行政处罚管理
 */
class AdministrativeController extends CController {
	/**
	 *
	 * @see 行政处罚列表
	 */
	function actionList() {
		$id = $_GET ['id'];
		$this->render ( 'list', array (
				'master_id' => $id 
		) );
	}
	
	/**
	 *
	 * @see Ajax 列表
	 */
	function actionAjax_administrative_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		$service = new AdministrativeEntity ();
		$pg = $_GET ['page']; // 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		
		$master_id = $_GET ['id'];
		$where = array (
				'deleted' => 0,
				'master_id' => $master_id 
		);
		$p_name = $_GET ['p_name'];
		
		if ($p_name != "") {
			$where ['p_name'] = "[code] like '%$p_name%' [code]";
		}
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		$ret->tct = $totalCount;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$ret->pi = $pg;
		if ($list === false) {
			$ret->status = 500;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 200;
			$index = 0;
			foreach ( $list as $item ) {
				$list [$index] ['category_str'] = AdministrativeCategory_str ( $item ['category'] );
				// sys_debug_log ( AdministrativeCategory_str ( $item ['category'] ) );
				$index ++;
			}
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 招投标管理-招标列表-增加招投标
	 */
	function actionAdd() {
		$category = $_GET ['ty'] or "";
		$master_id = $_GET ['mid'];
		$service = new AdministrativeMasterEntity ();
		$model = $service->get ( $master_id );
		
		if ($category == "" || ! is_numeric ( $category ) || $category < 1 || $category > 21) {
			$this->render ( "category_add", array (
					'master_id' => $master_id,
					'masterEntity' => $model 
			) );
		} else {
			$this->render ( "category_add_$category", array (
					'master_id' => $master_id,
					'masterEntity' => $model 
			) );
		}
	}
	
	/**
	 *
	 * @see 编辑
	 */
	public function actionEditor() {
		$id = $_GET ['id'];
		$service = new AdministrativeEntity ();
		$model = $service->get ( $id );
		fb ( $model );
		if (false === $model || is_array ( $model ) === false || count ( $model ) === 0) {
			exit ( "参数错误" );
		}
		$msService = new AdministrativeMasterEntity ();
		$masterEntity = $msService->get ( $model ['master_id'] );
		$this->render ( 'category_add_' . $model ['category'], array (
				'model' => $model,
				'master_id' => $model ['master_id'],
				'masterEntity' => $masterEntity 
		) );
	}
	
	/**
	 *
	 * @see ajax提交
	 *
	 */
	public function actionAjax_administrative_add() {
		$ret = new stdClass ();
		$ret->status = 401;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$id = $_POST ['id'];
		$contentsequence = $_POST ['contentsequence'];
		$category = $_POST ['category'];
		$be_punished_man = $_POST ['be_punished_man'];
		$master_id = $_POST ['master_id'];
		if ($id == "") {
			$fldvalue = array (
					'contentsequence' => $contentsequence,
					'category' => $category,
					'be_punished_man' => $be_punished_man,
					'deleted' => 0,
					'add_time' => date ( 'Y-m-d H:i:s', time () ),
					'master_id' => $master_id,
					'auto_no' => '' 
			);
			$service = new AdministrativeEntity (); // 定义用户业务数据库操作对象
			
			$model = $service->get_list ( '1', 1, array (
					'YEAR(add_time)' => date ( 'Y', time () ) 
			), array (
					'id' => 'desc' 
			), array (
					'auto_no' 
			) );
			$ct = "";
			if (is_array ( $model ) && count ( $model ) > 0) {
				$ct = mb_substr ( $model [0] ['auto_no'], 5 );
				fb ( mb_substr ( $model [0] ['auto_no'], 4 ) );
				fb ( mb_substr ( $model [0] ['auto_no'], 3 ) );
				fb ( mb_substr ( $model [0] ['auto_no'], 2 ) );
			} else {
				$ct = 0;
			}
			$fldvalue ['auto_no'] = date ( 'Y', time () ) . str_pad ( ++ $ct, 3, '0', STR_PAD_LEFT );
			$result = $service->insert ( $fldvalue );
			if (is_numeric ( $result ) && $result > 0) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
			echo json_encode ( $ret );
		} else {
			$fldvalue = array (
					'contentsequence' => $contentsequence,
					'be_punished_man' => $be_punished_man 
			);
			$service = new AdministrativeEntity (); // 定义用户业务数据库操作对象
			$result = $service->update ( $fldvalue, array (
					'id' => $id 
			) );
			if ($result === true) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
			echo json_encode ( $ret );
		}
	}
	
	/**
	 *
	 * @see ajax 删除行政处罚信息
	 */
	function actionAjax_administrative_delete() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new AdministrativeEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	
	/**
	 * @行政处罚详情
	 */
	public function actionDetail() {
		if (isset ( $_GET ['id'] ) && is_numeric ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$service = new AdministrativeEntity ();
			$model = $service->get ( $id );
			if (false === $model || is_array ( $model ) === false || count ( $model ) == 0) {
				// 参数错误
				exit ( "参数错误" );
			} else {
				fb ( $model );
				$this->render ( 'category_detail_' . $model ['category'], array (
						'model' => $model 
				) );
			}
		} else {
			exit ( "参数错误" );
		}
	}
	
	/**
	 *
	 * @see 行政处罚列表视图
	 */
	function actionMaster() {
		$this->render ( "master" );
	}
	
	/**
	 *
	 * @see 行政处罚数据查询Action
	 */
	function actionAjax_master_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		
		$service = new AdministrativeMasterEntity ();
		$pg = $_GET ['page'];
		
		$company = $_GET ['company'] or "";
		sys_debug_log ( $company );
		$where = array (
				'deleted' => 0 
		);
		if ($company != "") {
			$where ['company'] = "[code] like '%$company%'[code]";
		}
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_record_count ( $where );
		$ret->tct = $totalCount;
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$ret->pi = $pg;
		if ($adminList === false) {
			$ret->status = 500;
			$ret->data = "未找到匹配数据";
		} else {
			
			$ret->status = 200;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 管理员编辑界面视图
	 */
	function actionMasterEditor() {
		$id = $_GET ['id'];
		if (is_numeric ( $id ) && $id > 0) {
			$service = new AdministrativeMasterEntity ();
			$model = $service->get ( $id );
			if (false === $model) {
				echo "参数错误";
				return;
			}
		}
		return $this->render ( "mastereditor", array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see ajax 编辑管理员信息
	 */
	function actionAjax_master_editor() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		$id = $_POST ['id'];
		// 公司名称
		$company = $_POST ['company'];
		// 案由
		$case = $_POST ['case1'];
		$project_name = $_POST ['project_name'];
		$service = new AdministrativeMasterEntity ();
		if (is_numeric ( $id ) && $id > 0) {
			$model = $service->get ( $id );
			if (false === $model) {
				$ret->status = 500;
				$ret->data = "参数错误";
				echo json_encode ( $ret );
				return;
			}
			$fld = array (
					'company' => $company,
					'case1' => $case,
					'project_name' => $project_name 
			);
			$result = $service->update ( $fld, array (
					'id' => $id 
			) );
			if (false === $result) {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			}
		} else {
			$ct = "";
			$model = $service->get_list ( '1', 1, array (
					'YEAR(add_time)' => date ( 'Y', time () ) 
			), array (
					'id' => 'desc' 
			), array (
					'auto_no' 
			) );
			if (is_array ( $model ) && count ( $model ) > 0) {
				$ct = mb_substr ( $model [0] ['auto_no'], 3 );
			} else {
				$ct = 0;
			}
			$fld = array (
					'company' => $company,
					'case1' => $case,
					'deleted' => 0,
					'add_time' => date ( 'Y-m-d H:i:s', time () ),
					'project_name' => $project_name,
					'auto_no' => date ( 'Y', time () ) . str_pad ( ++ $ct, 3, '0', STR_PAD_LEFT ) 
			);
			$result = $service->insert ( $fld );
			if (is_numeric ( $result ) && $result > 0) {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			}
		}
	}
	
	/**
	 *
	 * @see ajax 删除行政处罚信息
	 */
	function actionAjax_master_delete() {
		$ret = new stdClass ();
		$ret->status = 500;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new AdministrativeMasterEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
}

	
